package com.cloudsigns.factorymanager2.produce.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderBean;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderAssignRemoveView;
import com.cloudsigns.factorymanager2.produce.iview.IProductOrderAssignView;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderAssignPresenter;
import com.cloudsigns.factorymanager2.produce.presenter.ProductOrderAssignRemovePresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

public class ProduceDispatchWorkPartOrderActivity1 extends BaseActivity implements IProductOrderAssignView, IProductOrderAssignRemoveView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_workpart_name,tv_product_name,  tv_num,tv_size,tv_side_size, tv_process_cycle,tv_dispatch_way,tv_receive_people;
    private ImageView img_arrow_down,img_edit;
    private LinearLayout ll_content,ll_dispatch_way;
    private Button btn_dispatch_order, btn_delete_dispatch;

    private WorkPartBean workPartBean;
    private WorkPartOrderBean workPartOrderBean;

    private ProductOrderAssignPresenter productOrderAssignPresenter;
    private ProductOrderAssignRemovePresenter productOrderAssignRemovePresenter;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_dispatch_workpart_order1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        tv_workpart_name = findViewById(R.id.tv_workpart_name);
        tv_product_name=findViewById(R.id.tv_product_name);
        img_arrow_down=findViewById(R.id.img_arrow_down);
        ll_content=findViewById(R.id.ll_content);
        img_edit=findViewById(R.id.img_edit);
        ll_dispatch_way=findViewById(R.id.ll_dispatch_way);
        tv_num = findViewById(R.id.tv_num);
        tv_size=findViewById(R.id.tv_size);
        tv_side_size=findViewById(R.id.tv_side_size);
        tv_process_cycle=findViewById(R.id.tv_process_cycle);
        tv_dispatch_way = findViewById(R.id.tv_dispatch_way);
        tv_receive_people = findViewById(R.id.tv_receive_people);
        btn_dispatch_order = findViewById(R.id.btn_dispatch_order);
        btn_delete_dispatch = findViewById(R.id.btn_delete_dispatch);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.order_dispatch));
        img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
            workPartOrderBean = (WorkPartOrderBean) intent.getSerializableExtra(ConstantUtil.KEY_2);
        }

        img_edit.setImageResource(R.mipmap.edit_device_pressed);

        tv_workpart_name.setText(workPartBean.getWorkPartName());
        tv_product_name.setText(" /"+workPartBean.getProductName());
        tv_num.setText(workPartBean.getAmount() + workPartBean.getUnit());
        CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
        if (null != shape) {
            if (shape == CommonEnum.ProductShapeType.ROUND) {
                tv_size.setText("直径" + workPartBean.getShapeType().getDiameter() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                tv_size.setText("长度" + workPartBean.getShapeType().getWidth() + "mm*" + "高度" + workPartBean.getShapeType().getHeight() + "mm*" + "厚度" + workPartBean.getShapeType().getThick() + "mm");
            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                tv_size.setText(workPartBean.getShapeType().getDesc());
            }
        }
        tv_side_size.setText("上"+workPartBean.getShapeType().getBorderTop()+"mm"+"下"+workPartBean.getShapeType().getBorderBottom()+"mm"+"左"+workPartBean.getShapeType().getBorderLeft()+"mm"+"右"+workPartBean.getShapeType().getBorderRight()+"mm");
        tv_process_cycle.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate())+"—"+DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));

        tv_dispatch_way.setText("内派");
        tv_receive_people.setText(workPartOrderBean.getAssignReceiveAccountName() + ":" + workPartOrderBean.getAssignReceiveAccountId());

        productOrderAssignPresenter = new ProductOrderAssignPresenter(this);
        productOrderAssignRemovePresenter = new ProductOrderAssignRemovePresenter(this);

        if (TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId()) || workPartOrderBean.getAssignReceiveStatus() == null || workPartOrderBean.getAssignReceiveStatus() == CommonEnum.ProduceOrderStatus.CREATED) {
            btn_dispatch_order.setBackgroundResource(R.drawable.btn_green_submit);
        } else {
            ll_dispatch_way.setVisibility(View.VISIBLE);
            img_edit.setImageResource(R.mipmap.edit_device_normal);
            btn_dispatch_order.setBackgroundResource(R.mipmap.btn_long_bg_gray);
            btn_dispatch_order.setEnabled(false);
            img_edit.setEnabled(false);
        }
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_arrow_down.setOnClickListener(this);
        img_edit.setOnClickListener(this);
        btn_dispatch_order.setOnClickListener(this);
        btn_delete_dispatch.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 收放
            case R.id.img_arrow_down:
                if (ll_content.getVisibility() == View.VISIBLE) {
                    ll_content.setVisibility(View.GONE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_down_icon);
                } else {
                    ll_content.setVisibility(View.VISIBLE);
                    img_arrow_down.setBackgroundResource(R.mipmap.arrow_top);
                }
                break;
            // 编辑工序派工信息
            case R.id.img_edit:
                   Intent intent = new Intent(ProduceDispatchWorkPartOrderActivity1.this, ProduceEditDispatchInformationActivity1.class);
                    intent.putExtra(ConstantUtil.KEY_1, CommonEnum.ROLECATEGORY.TEAM);
                    intent.putExtra(ConstantUtil.KEY_2, workPartBean);
                    intent.putExtra(ConstantUtil.KEY_3, workPartOrderBean.getAmount());
                    startActivityForResult(intent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            // 工单分派
            case R.id.btn_dispatch_order:
                productOrderAssignPresenter.productOrderAssign();
                break;
            // 删除派工
            case R.id.btn_delete_dispatch:
                LayoutInflater inflater = getLayoutInflater();
                View view1 = inflater.inflate(R.layout.dialog_delete, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                TextView tv_content = view1.findViewById(R.id.tv_content);
                final Dialog dialog = new Dialog(ProduceDispatchWorkPartOrderActivity1.this);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);
                tv_content.setText("确定删除给【" + workPartOrderBean.getAssignReceiveAccountName() + "】的派工吗？");

                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        if (!TextUtils.isEmpty(workPartOrderBean.getWorkPartOrderId()) && workPartOrderBean.getAssignReceiveStatus() != null) {
                            productOrderAssignRemovePresenter.productOrderAssignRemove();
                        } else {
                            workPartBean.removeWorkpartOrderByIdOrOtherMessage(workPartOrderBean);
                            Intent mIntent = new Intent();
                            mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
                            setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
                            showToast("删除成功");
                            finish();
                        }

                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (null != data) {
                    WorkPartOrderBean wpOrderBean = (WorkPartOrderBean) data.getSerializableExtra(ConstantUtil.KEY_2);
                    ll_dispatch_way.setVisibility(View.VISIBLE);
                    tv_dispatch_way.setText("内派");
                    tv_receive_people.setText(wpOrderBean.getAssignReceiveAccountName() + ":" + wpOrderBean.getAssignReceiveAccountId());
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public int getAmount() {
        return workPartOrderBean.getAmount();
    }

    @Override
    public String getAssignReceiveAccountId() {
        return workPartOrderBean.getAssignReceiveAccountId();
    }

    @Override
    public String getOrderId() {
        return workPartBean.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartBean.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartBean.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderBean.getWorkPartOrderId();
    }

    @Override
    public void getProductOrderAssignRemoveSuccessed() {
        workPartBean.removeWorkpartOrderByIdOrOtherMessage(workPartOrderBean);
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        showToast("删除成功");
        finish();
    }

    @Override
    public void getProductOrderAssignRemoveFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void getProductOrderAssignSuccessed(WorkPartOrderBean wpBean) {
        for (WorkPartOrderBean wk : workPartBean.getWorkPartOrderList()) {
            if (wk.getWorkPartOrderId().equals(workPartOrderBean.getWorkPartOrderId())) {
                wk.setAssignReceiveStatus(CommonEnum.ProduceOrderStatus.EDITED);
                wk.setWorkPartOrderId(wpBean.getWorkPartOrderId());
            }
        }
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, workPartBean);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        showToast("分派成功");
        finish();
    }

    @Override
    public void getProductOrderAssignFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
